package task5;

import java.util.*;

/**
 * 斗地主游戏类
 */
public class PokerGame {
    private List<String> pokers;

    public List<String> getPokers() {
        return pokers;
    }

    public void setPokers(List<String> pokers) {
        this.pokers = pokers;
    }

    // 对54张扑克牌进行洗牌
    public void shuffle(){
        Poker poker = new Poker();
        // 添加52张扑克
        pokers = poker.getPokers();
        for (CardSuit sd:poker.getCardSuit()){
            for (String s:poker.getNumber()){
                pokers.add(sd.getCardSuit() + s);
            }
        }
        for (Joker jk:poker.getJoker())
            pokers.add(jk.getJoker());

        Collections.shuffle(pokers);
        this.setPokers(pokers);
    }

    // 玩家摸牌,由于是交替摸牌，所以每次返回1张
    public String getPoker(){
        // 定义返回扑克
        String get_poker = new String();
        // 使用迭代器遍历
        ListIterator<String> listIterator = this.getPokers().listIterator();
        if (listIterator.hasNext()){
            get_poker = listIterator.next();
            listIterator.remove();
        }
        return get_poker;
    }

    // 对玩家手里的牌进行排序，规则为：大王、小王、2、A、K、Q、J、10、9、8、7、6、5、4、3
    public List<String> sortPokers(List<String> pokers){
        // 定义一个HashMap用来存储当前玩家手里牌中，不同数字的花色种类
        HashMap<String,List<String>> hashMap = new HashMap<>();
        for (String poker:pokers){
            if (!poker.equals("大王") && !poker.equals("小王")){
                String key = poker.substring(2,poker.length());
                String value = poker.substring(0,2);
                if (!hashMap.containsKey(key)){
                    List<String> list = new ArrayList<>();
                    list.add(value);
                    hashMap.put(key,list);
                }
                else
                    hashMap.get(key).add(value);
            }
        }
        /*for (String string:hashMap.keySet()){
            System.out.print(string + ": ");
            for (String s : hashMap.get(string)){
                System.out.print(s + " ");
            }
            System.out.println();
        }*/
        // 定义暂存List，存储转换为数字的牌
        List<Integer> temp_pokers = new ArrayList<>();
        // 先去掉花色，提取出牌中的数字，不是数字的转换为数字
        for (String poker:pokers){
            if (poker.equals("大王"))
                temp_pokers.add(17);
            else if (poker.equals("小王"))
                temp_pokers.add(16);
            else{
                String sub_poker = poker.substring(2,poker.length());
                switch (sub_poker) {
                    case "2":
                        temp_pokers.add(15);
                        break;
                    case "A":
                        temp_pokers.add(14);
                        break;
                    case "K":
                        temp_pokers.add(13);
                        break;
                    case "Q":
                        temp_pokers.add(12);
                        break;
                    case "J":
                        temp_pokers.add(11);
                        break;
                    case "10":
                        temp_pokers.add(10);
                        break;
                    case "9":
                        temp_pokers.add(9);
                        break;
                    case "8":
                        temp_pokers.add(8);
                        break;
                    case "7":
                        temp_pokers.add(7);
                        break;
                    case "6":
                        temp_pokers.add(6);
                        break;
                    case "5":
                        temp_pokers.add(5);
                        break;
                    case "4":
                        temp_pokers.add(4);
                        break;
                    case "3":
                        temp_pokers.add(3);
                        break;
                    default:
                        break;
                }
            }
        }
        // 对牌进行从大到小排序
        Collections.sort(temp_pokers, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2 - o1;
            }
        });

        // 最终返回结果List
        List<String> sorted_pokers = new ArrayList<>();
        ListIterator<Integer> listIterator = temp_pokers.listIterator();
        while (listIterator.hasNext()){
            String string = String.valueOf(listIterator.next());
            if (string.equals("17")){
                sorted_pokers.add("大王");
                listIterator.remove();
            }else if (string.equals("16")){
                sorted_pokers.add("小王");
                listIterator.remove();
            }else {
                switch (string) {
                    case "15":
                        sorted_pokers.add("2");
                        listIterator.remove();
                        break;
                    case "14":
                        sorted_pokers.add("A");
                        listIterator.remove();
                        break;
                    case "13":
                        sorted_pokers.add("K");
                        listIterator.remove();
                        break;
                    case "12":
                        sorted_pokers.add("Q");
                        listIterator.remove();
                        break;
                    case "11":
                        sorted_pokers.add("J");
                        listIterator.remove();
                        break;
                    case "10":
                        sorted_pokers.add("10");
                        listIterator.remove();
                        break;
                    case "9":
                        sorted_pokers.add("9");
                        listIterator.remove();
                        break;
                    case "8":
                        sorted_pokers.add("8");
                        listIterator.remove();
                        break;
                    case "7":
                        sorted_pokers.add("7");
                        listIterator.remove();
                        break;
                    case "6":
                        sorted_pokers.add("6");
                        listIterator.remove();
                        break;
                    case "5":
                        sorted_pokers.add("5");
                        listIterator.remove();
                        break;
                    case "4":
                        sorted_pokers.add("4");
                        listIterator.remove();
                        break;
                    case "3":
                        sorted_pokers.add("3");
                        listIterator.remove();
                        break;
                    default:
                        break;
                }
            }
        }

        // 给排好序的牌添加原来的花色
        List<String> result = new ArrayList<>();
        for (String poker:sorted_pokers){
            if (!poker.equals("大王") && !poker.equals("小王")){
                ListIterator<String> listIterator1 = hashMap.get(poker).listIterator();
                while (listIterator1.hasNext()){
                    result.add(listIterator1.next() + poker);
                    listIterator1.remove();
                }
            }else
                result.add(poker);
        }
        return result;
    }

    // 开始玩斗地主
    public static void play(){
        // 1.创建扑克牌游戏
        PokerGame pokerGame = new PokerGame();
        System.out.println("开始洗牌........");
        // 2.准备54张扑克牌并洗牌
        pokerGame.shuffle();
        // 3.创建三个玩家，轮流交替摸牌，每个玩家有个标识符，true表示继续抓牌，false表示停止抓牌
        Player player1 = new Player("玩家1");
        boolean flag1 = true;
        Player player2 = new Player("玩家2");
        boolean flag2 = true;
        Player player3 = new Player("玩家3");
        boolean flag3 = true;
        System.out.println("---------------------------玩家开始摸牌---------------------------");
        while (flag1 || flag2 || flag3){
            // 当玩家1手里的牌的个数小于17时，一直摸牌
            if (player1.getPlay_pokers() == null || player1.getPlay_pokers().size() < 17){
                System.out.println(player1.getName() + "摸牌，" + "摸到了"+pokerGame.getPokers().get(0));
                player1.add(pokerGame.getPoker());
            }
            else
                flag1 = false;
            // 当玩家2手里的牌的个数小于17时，一直摸牌
            if (player2.getPlay_pokers() == null || player2.getPlay_pokers().size() < 17){
                System.out.println(player2.getName() + "摸牌，" + "摸到了"+pokerGame.getPokers().get(0));
                player2.add(pokerGame.getPoker());
            }
            else
                flag2 = false;
            // 当玩家3手里的牌的个数小于17时，一直摸牌
            if (player3.getPlay_pokers() == null || player3.getPlay_pokers().size() < 17){
                System.out.println(player3.getName() + "摸牌，" + "摸到了"+pokerGame.getPokers().get(0));
                player3.add(pokerGame.getPoker());
            }
            else
                flag3 = false;
        }
        System.out.println("---------------------------玩家手里的原始牌-------------------------");
        // 4.显示三名玩家手里的牌
        player1.show();
        player2.show();
        player3.show();
        // 5.显示底牌
        System.out.println("-----------------------------剩余底牌-----------------------------");
        for (String s:pokerGame.getPokers())
            System.out.print(s + "  ");
        System.out.println();
        System.out.println("---------------------------玩家洗牌之后的牌-------------------------");
        // 6.对玩家手里的牌进行排序
        player1.setPlay_pokers(pokerGame.sortPokers(player1.getPlay_pokers()));
        player1.show();

        player2.setPlay_pokers(pokerGame.sortPokers(player2.getPlay_pokers()));
        player2.show();

        player3.setPlay_pokers(pokerGame.sortPokers(player3.getPlay_pokers()));
        player3.show();
    }
}
